Skip to content

Add generated inference for Permuto and multilevel LoD - #539

Open
shsolanki-1 wants to merge 6 commits into
NVlabs:masterfrom
shsolanki-1:codex/permuto-lod-implementation
Open

shsolanki-1 wants to merge 6 commits into
NVlabs:masterfrom
shsolanki-1:codex/permuto-lod-implementation

Conversation

@shsolanki-1

@shsolanki-1 shsolanki-1 commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Note

This PR depends on #538. GitHub shows the combined stack until #538 lands. The incremental review range is codex/permuto-lod-native...codex/permuto-lod-implementation.

Summary

  • Add generated device forward functions for Permuto and MultiLevelEncodingLoD.
  • Enable direct runtime compilation and automatic just-in-time (JIT) inference for Permuto-backed and Grid-backed LoD encodings.
  • Cache runtime-compiled kernels per CUDA context and unload each module in its owning context. A thread without a current CUDA context binds the current device's primary context before the cache lookup.
  • Disable generated execution when mutable multilevel controls would make cached source stale.
  • Propagate JIT-state validity through Composite and NetworkWithInputEncoding.
  • Preserve native half-precision sum and product behavior in generated composite encodings.

Motivation

Permuto and the generalized LoD wrapper provide native forward and derivative paths in #538, but they do not provide generated evaluators for fused inference. The existing runtime-compilation cache also stores one context-bound CUDA module per object. Reusing that module from another CUDA context is invalid.

This change adds the generated inference path and makes the shared runtime-compilation cache safe for objects used from multiple CUDA contexts, including PyTorch DataParallel inference.

Execution Contract

Generated support is limited to inference. The generated functions retain the standard three-argument device-function application binary interface and do not allocate a generated forward context.

Training falls back to the native forward and derivative paths before a JIT forward context is created. Generated backward and generated double backward remain unsupported.

Double backward after a JIT forward raises Cannot use native double backward with a JIT forward context. and disables JIT for the object, because no model provides generated double backward. Later iterations use the native path. Before this change, the same sequence failed with std::bad_cast.

Enabling JIT after it was disabled clears the object's cached kernels. With the PyTorch default runtime-compilation cache directory, the kernels reload from disk in about 1 ms. Without a cache directory, the next JIT call recompiles.

Restrictive scalar or per-element multilevel caps disable JIT for the affected object. This prevents cached generated source from embedding stale mutable state. Backward and double backward select their execution path from the supplied forward context instead of the object's current JIT setting.

Validation

  • Earlier revision: configured and built Release tests with CUDA 12.8, GCC 12.3, and TCNN_CUDA_ARCHITECTURES=89. Passed focused native Permuto and LoD coverage, generated-inference and JIT-context cases, CUDA runtime-compilation cache cases, and PyTorch generated-inference and two-GPU DataParallel coverage.
  • Current revision, restacked on the current Add native Permuto and multilevel LoD encodings #538 and built with CUDA 13.0 and TCNN_CUDA_ARCHITECTURES=120 on one RTX PRO 6000 Blackwell: test_encodings, test_grid, test_jit_losses, test_permuto (43 test cases), and test_rtc_device_cache (3 test cases, including JIT inference from a thread without a current CUDA context) pass. The two multi-GPU cache cases return early with a warning on this single-GPU machine. In test_networks, the existing JIT tolerance test Various invariance checks for neural networks fails with the same 200 assertions on the unmodified base commit; the other test cases pass.
  • Current revision, PyTorch bindings built with CUDA 12.8 and PyTorch 2.8 for sm_120: bindings/torch/tests/test_permuto.py passes 11 tests and skips the two-GPU DataParallel test.
  • Verified the PR range with git diff --check.

Register the native Permuto encoding and a hard MultiLevelEncodingLoD
wrapper for training, with parameter-gradient, input-gradient, and double
backward paths. Add non-JIT FullyFusedMLP double backward for one hidden
ReLU layer without an output activation, and let the torch bindings skip
the second-order input gradient when callers do not need it.

NetworkWithInputEncoding chains double backward through its encoding and
network. Network::backward takes the network output, which double
backward does not receive, so it is recomputed from the saved encoding
output on that path. Storing a copy in forward instead would cost every
pass that prepares first-order input gradients.

The torch bindings compute the second-order input gradient whenever the
input requires a gradient, including when the parameters are frozen.
tcnn.Network wraps its network in an Identity encoding, so Identity now
implements double backward: it passes the scaled second-order gradient
upstream, and its input Hessian is zero because it is linear. Without it,
frozen-parameter tcnn.Network double backward would fail in the encoding.
Dispatch the existing native implementation over the public dimension and
feature-width matrix. Canonicalize aliases and validate configuration
before template construction while preserving the existing 5D/two-feature
artifact contract.

Cap AoS transpose geometry so wide outputs stay within CUDA block-size
limits.
Wrap any multilevel encoding so Grid and Permuto share one hard or soft
LoD path. Preserve base layouts, strides, parameters, streams, and double
backward by staging incompatible matrices through the preferred layout and
retaining unweighted soft outputs.

Expose level counts through the minimal base interface and align Grid
derivative cutoffs with the forward predicate at the epsilon boundary.
A CUDA module is valid only in the context that loaded it, so JIT kernels
and MLP parameter-conversion kernels are cached per CUDA context and each
module is unloaded in its owning context. A thread without a current
context binds the current device's primary context before the lookup, as
its first CUDA runtime call would, so JIT inference still works from a
fresh thread.
Generate public inference evaluators for Permuto and wrapper-owned LoD masking through the standard device-function ABI.

Disable generated execution when mutable multilevel controls would make cached source stale. Propagate validity through composed models, preserve float reduction behavior, and select derivative execution from the supplied forward context.

Keep generated contexts and derivatives unsupported, so training disables JIT before context allocation and uses native forward and backward.
@shsolanki-1
shsolanki-1 force-pushed the codex/permuto-lod-implementation branch from c976ac7 to f645cac Compare September 29, 2026 10:16
@shsolanki-1
shsolanki-1 marked this pull request as ready for review September 30, 2026 05:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant